package com.hsc.www.filterBean;//package com.hsc.www.security01;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.GenericFilterBean;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Configuration
public class Config {


    @Bean
    public GenericFilterBean userTimeLogFilter() {

        GenericFilterBean genericFilterBean = new OncePerRequestFilter() {
            @Override
            protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
                Long start = System.currentTimeMillis();
                try {
                    filterChain.doFilter(request, response);
                } finally {
                    System.out.println(request.getRequestURI() + " 用时：" + (System.currentTimeMillis() - start) + "ms");
                }
            }
        };
        return genericFilterBean;
    }


}